﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SafeCalculations;

namespace SafeCalculations.UnitTests
{
    [TestClass]
    public class CalculationExtensionsUnitTests
    {
        [TestMethod]
        public void CalculateAverageResultNotNeedingABigInteger()
        {
            int[] testValues = new int[] { 1, 2, 3 };
            Assert.AreEqual(testValues.Average(), testValues.AverageNoOverflow());
        }

        [TestMethod]
        public void CalculatePositiveAverageResultNeedingABigInteger()
        { AssertRepeatedValueGivesCorrectAverage(int.MaxValue); }

        [TestMethod]
        public void CalculateNegativeAverageResultNeedingABigInteger()
        { AssertRepeatedValueGivesCorrectAverage(int.MinValue); }

        [TestMethod]
        public void CalculateAverageWithDecimal()
        {
            int[] testValues = new int[] { 1, 2 };
            Assert.AreEqual(testValues.Average(), testValues.AverageNoOverflow());
        }

        private void AssertRepeatedValueGivesCorrectAverage(int repeatedValue)
        { Assert.AreEqual(repeatedValue, Enumerable.Repeat<int>(repeatedValue, 5).ToArray().AverageNoOverflow()); }
    }
}
